import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ThymeleafGonfig implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        //1.创建engine实例
        TemplateEngine engine = new TemplateEngine();
        //2.创建resolver实例
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(context);
        resolver.setPrefix("/WEB-INF/template/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("utf-8");
        //3.把resolver和enigne关联起来
        engine.setTemplateResolver(resolver);
        //4.把engine放到ServletContext里面
        context.setAttribute("engine",engine);
        System.out.println("Template初始化成功");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
